package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.service.FeignSysGroupService;
import com.alibaba.algo.cooperation.feign.service.UserFeignService;
import com.alibaba.algo.cooperation.feign.vo.req.GetGroupBaseInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectGroupPageRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.SelectUserInfoReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.GroupBaseInfoResponseVO;
import com.alibaba.algo.cooperation.feign.vo.res.RpcUserInfoVO;
import com.alibaba.algo.cooperation.feign.vo.res.UserGroupResVO;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.UserGroupService;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.GroupInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.userentity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
　　* @description: 用户分组服务实现
　　* @author shenrm
　　* @date 2021/10/27 15:01
　　*/
@Slf4j
@Service
public class UserGroupServiceImpl implements UserGroupService {

    @Autowired
    private FeignSysGroupService feignSysGroupService;

    @Autowired
    private UserFeignService userFeignService;

    @Override
    public List<GroupInfo> getUserGroupList() {
        SelectGroupPageRequestVO requestVO = new SelectGroupPageRequestVO();
        requestVO.setPageIndex(1);
        requestVO.setPageSize(999);
        BaseResult  baseResult = feignSysGroupService.selectPage(requestVO);
        LinkedHashMap linkedHashMap = (LinkedHashMap)baseResult.getData();
        if(MapUtils.isEmpty(linkedHashMap)) {
            return Collections.emptyList();
        }
        List<LinkedHashMap> groupInfoMapList = (List<LinkedHashMap>) linkedHashMap.get("dataList");
        if(Objects.isNull(groupInfoMapList)) {
            return Collections.emptyList();
        }

        List<GroupInfo> groupInfos = new ArrayList<>();
        for (LinkedHashMap mapObj : groupInfoMapList){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setGroupCode(String.valueOf(mapObj.get("id")));
            groupInfo.setGroupName(String.valueOf(mapObj.get("groupName")));
            groupInfos.add(groupInfo);
        }
        return groupInfos;
    }

    @Override
    public List<GroupInfo> getGroupInfoByUserId(String userId) {
        if(StringUtils.isBlank(userId)) {
            return Collections.emptyList();
        }
        SelectUserInfoReqVO reqVO = new SelectUserInfoReqVO();
        reqVO.setUserId(Long.parseLong(userId));
        BaseResult<RpcUserInfoVO> rpcUserInfoVos = userFeignService.qryUserInfo(reqVO);
        if(Objects.isNull(rpcUserInfoVos)) {
            return Collections.emptyList();
        }
        RpcUserInfoVO rpcUserInfoVO = rpcUserInfoVos.getData();
        if(Objects.isNull(rpcUserInfoVO) ||
                Objects.isNull(rpcUserInfoVO.getUserGroupList())) {
            return Collections.emptyList();
        }

        List<GroupInfo> groupInfos = new ArrayList<>();
        for (UserGroupResVO vo : rpcUserInfoVO.getUserGroupList()){
            GroupInfo groupInfo = new GroupInfo();
            groupInfo.setGroupCode(String.valueOf(vo.getGroupId()));
            groupInfo.setGroupName(vo.getGroupName());
            groupInfos.add(groupInfo);
        }
        return groupInfos;
    }

    @Override
    public List<UserInfo> getUserListByGroupId(String groupCode, List<Long> groupCodes) {
        if(StringUtils.isBlank(groupCode) && CollectionUtils.isEmpty(groupCodes)) {
            return Collections.emptyList();
        }

        // 创建查询条件对象
        SelectGroupPageRequestVO requestVO = new SelectGroupPageRequestVO();

        // 用户组编码不为空时,则设置为查询条件
        if(StringUtils.isNotBlank(groupCode) && StringUtils.isNumeric(groupCode)) {
//            requestVO.setGroupCodeKey(groupCode);
            requestVO.setGroupId(Long.valueOf(groupCode));
        }

        // 用户组编码集合不为空时,则设置为查询条件
        if(CollectionUtils.isNotEmpty(groupCodes)) {
            requestVO.setGroupIdList(groupCodes);
        }
        requestVO.setPageIndex(1);
        requestVO.setPageSize(999);
        BaseResult baseResult = feignSysGroupService.selectPage(requestVO);
        LinkedHashMap linkedHashMap = (LinkedHashMap)baseResult.getData();
        if(MapUtils.isEmpty(linkedHashMap)) {
            return Collections.emptyList();
        }
        List<LinkedHashMap> groupInfoMapList = (List<LinkedHashMap>) linkedHashMap.get("dataList");
        if(Objects.isNull(groupInfoMapList)) {
            return Collections.emptyList();
        }

        // 用户信息列表
        List<UserInfo> userInfos = new ArrayList<>();
        for (LinkedHashMap mapObj : groupInfoMapList){
            List<LinkedHashMap> employeeMapList = (List<LinkedHashMap>) mapObj.get("employeeList");
            if(Objects.isNull(groupInfoMapList)) {
                return Collections.emptyList();
            }
            UserInfo userInfo;
            for (LinkedHashMap employeeMap : employeeMapList){
                if (Objects.nonNull(employeeMap.get("userId"))){
                    userInfo = new UserInfo();
                    userInfo.setUserCode(String.valueOf(employeeMap.get("userId")));
                    userInfo.setUserName(String.valueOf(employeeMap.get("userName")));
                    userInfos.add(userInfo);
                }

            }
        }
        return userInfos;
    }

    /**
     * 根据组织名称查询组织分页列表
     * @param groupName
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult getUserGroupPageList(String groupName, Integer pageNo, Integer pageSize) {
        List<GroupInfo> groupInfoList = new ArrayList<>();
        SelectGroupPageRequestVO vo = new SelectGroupPageRequestVO();
        if (StringUtils.isNotEmpty(groupName) && StringUtils.isNotBlank(groupName)) {
            vo.setGroupNameKey(groupName);
        }
        vo.setPageIndex(pageNo);
        vo.setPageSize(pageSize);
        BaseResult groupList = feignSysGroupService.selectPage(vo);
        Integer total = 0;
        if (groupList!=null && groupList.getData()!=null) {
            List listData = Collections.singletonList(((HashMap<String, String>) groupList.getData()).get("dataList"));
            int maxSize = ((ArrayList) listData.get(0)).size();
            total = (Integer) ((HashMap<String,Object>)groupList.getData()).get("total");
            String gCode="",gName="";
            for (int i=0; i<maxSize; i++) {
                GroupInfo info = new GroupInfo();
                gName = ((LinkedHashMap<String,Object>)((ArrayList) listData.get(0)).get(i)).get("groupName").toString();
                gCode = ((LinkedHashMap<String, Object>) ((ArrayList) listData.get(0)).get(i)).get("id").toString();
                info.setGroupName(gName);
                info.setGroupCode(gCode);
                groupInfoList.add(info);
            }
        }
        return ResponseResult.success(groupInfoList)
                .setTotalCount(total)
                .setPageNum(pageNo)
                .setPageSize(pageSize);
    }

    @Override
    public GroupInfo getGroupDetail(Long groupCode) {
        if(Objects.isNull(groupCode)) {
            return null;
        }
        SelectGroupPageRequestVO requestVO = new SelectGroupPageRequestVO();
        requestVO.setGroupId(groupCode);
        requestVO.setPageIndex(1);
        requestVO.setPageSize(1);
        BaseResult  baseResult = feignSysGroupService.selectPage(requestVO);
        LinkedHashMap linkedHashMap = (LinkedHashMap)baseResult.getData();
        if(MapUtils.isEmpty(linkedHashMap)) {
            return null;
        }

        List<LinkedHashMap> groupInfoMapList = (List<LinkedHashMap>) linkedHashMap.get("dataList");
        if(Objects.isNull(groupInfoMapList)) {
            return null;
        }

        LinkedHashMap dataMap = groupInfoMapList.get(0);
        if(MapUtils.isEmpty(dataMap)) {
            return null;
        }

        GroupInfo groupInfo = new GroupInfo();
        groupInfo.setGroupCode(String.valueOf(dataMap.get("id")));
        groupInfo.setGroupName(String.valueOf(dataMap.get("groupName")));
        // 群组编码
        groupInfo.setCode(String.valueOf(dataMap.get("groupCode")));
        return groupInfo;
    }

    @Override
    public GroupInfo getGroupDetailByGroupCode(String groupId, String groupCode, String isDeleted) {
        GetGroupBaseInfoReqVO requestVO = new GetGroupBaseInfoReqVO();

        // 不为空作为查询条件
        if(StringUtils.isNotBlank(groupId)) {
            requestVO.setGroupId(Long.parseLong(groupId));
        }
        if(StringUtils.isNotBlank(groupCode)) {
            requestVO.setGroupCode(groupCode);
        }
        if(StringUtils.isNotBlank(isDeleted)) {
            requestVO.setIsDeleted(isDeleted);
        }
        BaseResult<GroupBaseInfoResponseVO>  baseResult = feignSysGroupService.getGroupBaseInfoByIdOrCode(requestVO);
        if(Objects.isNull(baseResult)) {
            return null;
        }

        // 获取响应数据
        GroupBaseInfoResponseVO responseData = baseResult.getData();
        if(Objects.isNull(responseData)) {
            return null;
        }

        // 创建用户群组信息
        GroupInfo groupInfo = new GroupInfo();
        // 群组ID
        groupInfo.setGroupCode(String.valueOf(responseData.getGroupId()));
        groupInfo.setGroupName(responseData.getGroupName());
        // 群组编码
        groupInfo.setCode(responseData.getGroupCode());
        return groupInfo;
    }
}